package org.belkodevelop.token.dao;

import org.belkodevelop.token.domain.Token;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TokenDAOImpl implements TokenDAO {
    
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public Token getToken(String value) {
        return (Token)sessionFactory.getCurrentSession().
                createCriteria(Token.class).add(Restrictions.eq("value", value)).uniqueResult();
    }

    @Override
    public void removeToken(String value) {
        Query query = sessionFactory.getCurrentSession().
                createQuery("delete from Token where value = :value");
        query.setParameter("value", value);
        query.executeUpdate();     
    }

    @Override
    public void createToken(Token token) {
        sessionFactory.getCurrentSession().save(token);
    }
    
}
